package com.ycw.controller.wang;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycw.bean.Users;
import com.ycw.service.wang.UsersService;
import com.ycw.util.Msg;

@Controller
public class WangwangController {
	@Autowired

	@Qualifier("wUsersServiceImpl")
	private UsersService usersService;

	// 登录logincenter
	@RequestMapping("logincenter")
	public String login(Users users, HttpSession session) {
		System.out.println(users.getUsEmail() + users.getUsPassword());
		if (users.getUsEmail() == "" || users.getUsPassword() == "") {
			return "../login";
		} else {
			users = usersService.getUsersByCondition(users);
			if (users != null) {
				session.setAttribute("users", users);
				return "redirect:/index.jsp";
			}
			return "../login";
		}
	}

	// 查询所有用户
	@RequestMapping("/getUsers")
	@ResponseBody
	public Msg getUsersByUsrole(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		PageHelper.startPage(pn, 10);
		List<Users> users = usersService.getUsers();
		PageInfo pageInfo = new PageInfo(users, 10);
		return Msg.success().add("pageInfo", pageInfo);
	}

	// 根据用户名查询所有
	@RequestMapping("/getAllByUsId")
	@ResponseBody
	public Msg getAllByUsId(String usId) {
		Users users = usersService.getAllByUsId(Integer.parseInt(usId));
		return Msg.success().add("users", users);
	}

	// 根据id修改激活状态
	@RequestMapping("/updateUsRole")
	@ResponseBody
	public Msg updateUsRole(Users users) {
		users.setUsRole(1);
		boolean f = usersService.updateUsRole(users);
		return Msg.success().add("users", users);
	}
}
